package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.reception.ICustReceptionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.KiteCustomerInfoReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstAcct;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <Description> 过户信息-客户信息处理 <br>
 *
 * @author wei.yanhong<br>
 * @version 1.0<br>
 * @since R9.0<br>
 */
@Service
public class TransfCustDealHandler extends AbstractAttrHandler {

    @Autowired
    private ICustReceptionService custReceptionService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    /**
     * 修改客户属性
     */
    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        String newCustId = "";
        if (KiteStringUtils.isNotEmpty(value)) {

            // 过户页面选择客户时候，需要联动设置其他客户信息
            //1、根据客户ID 查询出客户详情
            //            CuccCustInfoDTO cuccCustInfo = custReceptionService.getCustInfoByCustId(value).getResultObject();
            KiteCustomerInfoReq kiteCustomerInfoReq = new KiteCustomerInfoReq();
            kiteCustomerInfoReq.setGroupId(value);
            String staffCode = ContextUtil.getSimpleLoginInfo().getStaffCode();
            String eparchyCode = ContextUtil.getSimpleLoginInfo().getLanId();
            String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
            kiteCustomerInfoReq.setStaffCode(staffCode);
            kiteCustomerInfoReq.setEparchyCode(eparchyCode);
            kiteCustomerInfoReq.setProvinceCode(provinceCode);
            kiteCustomerInfoReq.setPageNum(1);
            kiteCustomerInfoReq.setPageSize(10);
            // 调用查询客户列表

            PageInfo<CuccCustInfoDTO> list = custReceptionService.customerListQuery(kiteCustomerInfoReq);
            if (!KiteObjectUtils.isEmpty(list) && KiteListUtils.isNotEmpty(list.getList())) {
                List<CuccCustInfoDTO> cuccCustInfoDTOList = list.getList();
                CuccCustInfoDTO cuccCustInfoDTO = cuccCustInfoDTOList.get(0);
                if (Objects.nonNull(cuccCustInfoDTO)) {
                    String groupId = cuccCustInfoDTO.getGroupId();
                    String custLocation = cuccCustInfoDTO.getLocation();
                    newCustId = cuccCustInfoDTO.getCustId();
                    String eparchyName = cuccCustInfoDTO.getEparchyName();
                    cuccCustInfoDTO.setCustType(cuccCustInfoDTO.getCustClassType());
                    // 2 联动设置其他客户属性
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "groupId", groupId, groupId);
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "custId", value, value);
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "location", custLocation,
                        LocalAttrHelper.getRegionName(cuccCustInfoDTO.getProvinceCode()) + "-" + LocalAttrHelper
                            .getRegionName(cuccCustInfoDTO.getEparchyCode()));

                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "acctName", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "acctContractCode", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "provinceAcctId", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "accountId", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "cbssAcctId", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "openDate", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "provinceCode", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "eparchyCode", "", "");
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, "payMode", "", "");
                    // 联动付费关系中的账户信息
                    operateSceneInstService
                        .putExtParameter(sceneInstId, "offerInstAcctRels", new ArrayList<KiteOfferInstAcct>());
                    // 3 将过户到的新客户信息塞到缓存 ，后面提交订单转单需要该客户信息
                    operateSceneInstService
                        .putExtParameter(sceneInstId, sceneInstId + "_transNewCustDTO", cuccCustInfoDTO);
                }
            }
        }
        return super.apply(sceneInstId, fieldName, newCustId, valueDesc);
    }
}
